package org.example.student_dormitory.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

//这是一个过滤器
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        String requestURI = req.getRequestURI();

        //getSession()方法,如果有session则会返回,没有则会创建
        HttpSession session = req.getSession(false);

        if ("/student_dormitory_war_exploded/index.html".equals(requestURI) || session != null){
            chain.doFilter(request,response);
        }else {
            //不放行,跳转到登录页面
            resp.sendRedirect("/student_dormitory_war_exploded/index.html");
        }
    }

}
